package utils

import (
	"io/ioutil"

	"gopkg.in/yaml.v2"
)

//DbConfig 数据库配置
type DbConfig struct {
	Driver  string
	Connect string
}

type RedisConfig struct {
	Address  string
	Password string
}

type Alarm struct {
	Interval int
	Numbers  int
	Email    int64
	Contact  []string
}

type Email struct {
	Host     string
	Port     int
	Username string
	Password string
}

//Config 配置文件
type Config struct {
	Database DbConfig
	Redis    RedisConfig
	Other    map[string]string
	Alarm    Alarm
	Email    Email
}

var config *Config

//ReadConfig 读取配置文件
func InitConfig(source string) error {
	configBytes, err := ioutil.ReadFile(source)
	if err != nil {
		return err
	}
	err = yaml.Unmarshal(configBytes, &config)
	if err != nil {
		return err
	}
	return nil
}

//GetConfig 获取配置文件
func GetConfig() *Config {
	return config
}
